package com.zhangye.test.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import com.zhangye.test.entity.EasyArResult;
import com.zhangye.test.entity.Results;
import com.zhangye.test.entity.SimilarResult;
import com.zhangye.test.mapper.ArCommentMapper;
import com.zhangye.test.mapper.ArRecommendMapper;
import com.zhangye.test.mapper.ArReportMapper;
import com.zhangye.test.mapper.CategoryMapper;
import com.zhangye.test.mapper.CompanyMapper;
import com.zhangye.test.mapper.EasyArMapper;
import com.zhangye.test.mapper.InfomationMapper;
import com.zhangye.test.mapper.InterviewMapper;
import com.zhangye.test.mapper.RegionMapper;
import com.zhangye.test.mapper.ScanCountMapper;
import com.zhangye.test.mapper.SearchLogMapper;
import com.zhangye.test.model.Admin;
import com.zhangye.test.model.ArComment;
import com.zhangye.test.model.Category;
import com.zhangye.test.model.Company;
import com.zhangye.test.model.EasyAr;
import com.zhangye.test.model.Infomation;
import com.zhangye.test.model.Interview;
import com.zhangye.test.model.SearchLog;
import com.zhangye.test.util.AddressUtil;
import com.zhangye.test.util.PageInfo;
import com.zhangye.test.util.cloudUtils;

/**
 * easyAr
 * 作者： 张叶
 * 创建时间：2018年5月11日  上午11:56:04
 */
@Service
public class EasyArService {
	
	@Autowired
	private EasyArMapper easyArMapper;
	@Autowired
	private QiniuService qiniuService;
	@Autowired
	private CompanyMapper companyMapper;
	@Autowired
	private InterviewMapper interviewMapper;
	@Autowired
	private ArCommentMapper arCommentMapper;
	@Autowired
	private ScanCountMapper scanCountMapper;
	@Autowired
	private AdminService adminService;
	@Autowired
	private SearchLogMapper searchLogMapper;
	@Autowired
	private InfomationMapper infomationMapper;
	@Autowired
	private ArReportMapper arReportMapper;
	@Autowired
	private RegionMapper regionMapper;
	@Autowired
	private CategoryMapper categoryMapper;
	
	@Autowired
	private ArRecommendMapper arRecommendMapper;
	
	
	
	public int selectByWhereCount(String where){
		int i  = easyArMapper.selectByWhereCount(where);
		return i;
	}
	
	
	
	public List<Map<String,Object>> selectWhere(String where){
		List<Map<String,Object>> list = easyArMapper.selectWhere(where);
		return list;
	}


	/**
	 * 分类列表
	 * 作者： 张叶
	 * 创建时间：2018年10月30日  下午4:42:07
	 */
	@Transactional
	public List<Map<String,Object>> selectByPageCategory(PageInfo pageInfo, String where, String order,Admin admin) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("myCount", pageInfo.getCount());
		param.put("pageSize", pageInfo.getPageSize());
		param.put("page", pageInfo.getPage());
		param.put("where", where);
		param.put("order", order);
		if(admin != null && !"".equals(admin)){
			param.put("adminId", admin.getId());
		}else{
			param.put("adminId", 0);
		}
		List<Map<String, Object>> m = easyArMapper.selectByPageCategory(param);
		pageInfo.setCount(Integer.parseInt(param.get("myCount").toString()));
		return m;
	}

	
	/**
	 * 列表
	 * 作者： 张叶
	 * 创建时间：2018年5月23日  上午9:05:40
	 */
	public List<Map<String,Object>> selectByPage(PageInfo pageInfo, String where, String order) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("myCount", pageInfo.getCount());
		param.put("pageSize", pageInfo.getPageSize());
		param.put("page", pageInfo.getPage());
		param.put("where", where);
		param.put("order", order);
		List<Map<String, Object>> m = easyArMapper.selectByPage(param);
		pageInfo.setCount(Integer.parseInt(param.get("myCount").toString()));
		return m;
	}

	
	/**
	 * 点赞次数
	 * 作者： 张叶
	 * 创建时间：2018年11月2日  上午9:45:02
	 */
	public List<Map<String,Object>> selectByPageCounts(PageInfo pageInfo, String where, String order,String lat,String lng) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("myCount", pageInfo.getCount());
		param.put("pageSize", pageInfo.getPageSize());
		param.put("page", pageInfo.getPage());
		param.put("where", where);
		param.put("order", order);
		param.put("lat", lat);
		param.put("lng", lng);
		List<Map<String, Object>> m = easyArMapper.selectByPageCounts(param);
		pageInfo.setCount(Integer.parseInt(param.get("myCount").toString()));
		return m;
	}
	
	
	/**
	 * 新列表
	 * 作者： 张叶
	 * 创建时间：2018年5月23日  上午9:05:40
	 */
	@Transactional
	public List<Map<String,Object>> selectByPageApiNew(PageInfo pageInfo, String where, String order,String lat,String lng,Admin admin) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("myCount", pageInfo.getCount());
		param.put("pageSize", pageInfo.getPageSize());
		param.put("page", pageInfo.getPage());
		param.put("where", where);
		param.put("order", order);
		param.put("lat", lat);
		param.put("lng", lng);
		if(admin != null && !"".equals(admin)){
			param.put("adminId", admin.getId());
		}else{
			param.put("adminId", 0);
		}
		List<Map<String, Object>> m = easyArMapper.selectByPageApiNew(param);
		pageInfo.setCount(Integer.parseInt(param.get("myCount").toString()));
		return m;
	}
	
	
	/**
	 * 修改参数
	 * 作者： 张叶
	 * 创建时间：2018年8月31日  下午2:03:29
	 */
	public int updateNormal(EasyAr ea){
		int flag = easyArMapper.updateByPrimaryKey(ea);
		return flag;
	}


	/**
	 * 修改图片
	 * 检查相似度，
	 * 作者： 张叶
	 * 创建时间：2018年8月28日  上午10:43:47
	 * @throws Exception 
	 * @throws IOException 
	 */
	@Transactional
	public int updateVideoUrl(Long easyArId, MultipartFile trackingImage) throws Exception {
		int i = 0;
		EasyAr ea = easyArMapper.selectByPrimaryKey(easyArId);
		if(ea != null && !"".equals(ea)){
			String videoUrl = qiniuService.upload("file", trackingImage.getOriginalFilename(),trackingImage.getBytes());
			EasyArResult er = cloudUtils.updateImageUrl(ea.getTargetId(),encode(videoUrl.getBytes()));
			if(er != null && !"".equals(er)){
				String status = er.getStatusCode();
				if(status.equals("0")){//添加成功，入库
					ea.setVideoUrl(videoUrl);
					i = easyArMapper.updateByPrimaryKey(ea);
					if(i == 0){
						TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					}
				}
			}
		}
		return i;
	}


	/**
	 * 修改图片
	 * 检查相似度，
	 * 作者： 张叶
	 * 创建时间：2018年8月28日  上午10:43:47
	 * @throws Exception 
	 * @throws IOException 
	 */
	@Transactional
	public int updateImageUrl(Long easyArId, MultipartFile trackingImage) throws Exception {
		int i = 0;
		SimilarResult sr = cloudUtils.similarCloud(encode(trackingImage.getBytes()));
		List<Results> list = sr.getSimilar().getResults();
		if(list.size() == 0){//无相似图片  encode(imageUrl.getBytes()
			EasyAr ea = easyArMapper.selectByPrimaryKey(easyArId);
			if(ea != null && !"".equals(ea)){
				String imageUrl = qiniuService.upload("file", trackingImage.getOriginalFilename(),trackingImage.getBytes());
				EasyArResult er = cloudUtils.updateImageUrl(ea.getTargetId(),encode(trackingImage.getBytes()));
				if(er != null && !"".equals(er)){
					String status = er.getStatusCode();
					if(status.equals("0")){//添加成功，入库
						ea.setImageUrl(imageUrl);
						ea.setActive(1);
						i = easyArMapper.updateByPrimaryKey(ea);
						if(i == 0){
							TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
						}
					}
				}
			}
		}
		return i;
	}


	/**
	 * APP搜索，同时添加搜索记录
	 * 作者： 张叶
	 * 创建时间：2018年8月16日  上午8:58:40
	 */
	@Transactional
	public List<Map<String,Object>> selectResultNew(PageInfo pageInfo, String keywords, String type,Admin admin) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		String where = "";
		String order = " order by create_at desc";
		if(type == null || "".equals(type) || "0".equals(type)){
			where += " (name like '%" + keywords + "%' or info like '%" + keywords + "%')";
			where += " and status = 'on'";
			list = this.selectByPageCategory(pageInfo, where, order,admin);
		}else{
			order = " order by createDate desc";
			where += " username like '%" + keywords + "%'";
			list = adminService.selectByPage(pageInfo,where, order);
			if(list.size() > 0){
				String adminIds = "";
				for(int i = 0 ; i < list.size() ; i ++){
					adminIds = adminIds + list.get(i).get("id") + ",";
				}
				adminIds = adminIds.substring(0,adminIds.length()-1);
				where = " admin_id in (" + adminIds + ")";
			}else{
				where = " 1 = 2";
			}
			order = " order by create_at desc";
			where += " and status = 'on'";
			list = this.selectByPageCategory(pageInfo, where, order,admin);
		}
		SearchLog sl = new SearchLog();
		String wheres = " and name = '" + keywords + "' and type = '" + type + "'";
		if(admin != null){
			sl.setAdminId(admin.getId());
			wheres += " and admin_id = " + admin.getId();
		}
		List<SearchLog> lists = searchLogMapper.selectByWhere(wheres);
		if(lists.size() == 0){
			sl.setName(keywords);
			sl.setCreatedAt(new Date());
			if(type == null || "".equals(type) || "0".equals(type)){
				sl.setType("0");
			}else{
				sl.setType("1");
			}
			searchLogMapper.insert(sl);
		}
		return list;
	}
	

	
	
	/**
	 * 添加
	 * 作者： 张叶
	 * 创建时间：2018年5月15日  下午2:07:47
	 */
	public int insertCompany (Company c, MultipartFile trackingImage, MultipartFile meta) throws Exception{
		//上传七牛云
		int flag = 1;
		String uuid = UUID.randomUUID().toString();
		String videoUrl = qiniuService.upload("file", meta.getOriginalFilename(),meta.getBytes());
		videoUrl = videoUrl +  "," + uuid;
		
		String imageUrl = qiniuService.upload("file", trackingImage.getOriginalFilename(),trackingImage.getBytes());
		//添加easyAr云
		EasyArResult er = cloudUtils.addClouds(encode(trackingImage.getBytes()), c.getName(), 20, encode(videoUrl.getBytes()));
		if(er != null && !"".equals(er)){
			String status = er.getStatusCode();
			if(status.equals("0")){//添加成功，入库
				c.setTargetId(er.getResult().getTargetId());
				c.setImageUrl(imageUrl);
				c.setVideoUrl(videoUrl);
				c.setActive(er.getResult().getActive());
				c.setCreateAt(new Date());
				c.setUpdateDate(new Date());
				c.setImageUuid(uuid);
				if(companyMapper.insert(c) > 0){
					flag = 0;
				}
			}
		}
		return flag;
	}	
	

	
	/**
	 * 查找
	 * 作者： 张叶
	 * 创建时间：2018年5月23日  上午9:05:58
	 */
	public EasyAr selectByPrimaryKey(Long id ){
		EasyAr ea= easyArMapper.selectByPrimaryKey(id);
		return ea;
	}
	
	@Transactional
	public EasyAr selectInfoById(Long id,String uuid,Admin admin){
		EasyAr ea = easyArMapper.selectInfoById(id);
		
		if(admin != null && !"".equals(admin)){
			String wheres = " and admin_id = " + admin.getId() + " and type = 'give' and easy_ar_id = " + id;
			List<Infomation> lists = infomationMapper.selectByWhere(wheres);
			if(lists.size() > 0){
				ea.setRemark("1");
			}
		}
		
		//添加页面访问次数
		String where = " and easy_ar_id = " + id + " and telphone_uuid = '" + uuid + "'  and  TO_DAYS(created_at) = TO_DAYS(NOW())";
		List<Interview> list = interviewMapper.selectByWhere(where);
		if(list.size() == 0){
			Interview iv = new Interview();
			iv.setCreatedAt(new Date());
			iv.setEasyArId(id);
			iv.setTelphoneUuid(uuid);
			interviewMapper.insert(iv);
		}
		
		return ea;
	}
	
	public List<EasyAr> selectAll(){
		List<EasyAr> list = easyArMapper.selectAll();
		return list;
	}
	
	
	public List<EasyAr> selectByWhere(String where){
		List<EasyAr> list = easyArMapper.selectByWhere(where);
		return list;
	}
	
	
	/**
	 * 添加
	 * 1，检测相似度
	 * 2，添加
	 * 作者： 张叶
	 * 创建时间：2018年8月23日  上午9:16:55
	 */
	public int insert (EasyAr ea, MultipartFile trackingImage) throws Exception{
		//上传七牛云
		int flag = 1;
		SimilarResult sr = cloudUtils.similarCloud(encode(trackingImage.getBytes()));
		List<Results> list = sr.getSimilar().getResults();
		if(list.size() == 0){//无相似图片
			String imageUrl = qiniuService.upload("file", trackingImage.getOriginalFilename(),trackingImage.getBytes());
			//添加easyAr云
			EasyArResult er = cloudUtils.addClouds(encode(trackingImage.getBytes()), ea.getName(), 20, encode(ea.getVideoUrl().getBytes()));
			if(er != null && !"".equals(er)){
				String status = er.getStatusCode();
				if(status.equals("0")){//添加成功，入库
					flag = insertEasyAr(ea, flag, imageUrl, er);
					if(flag != 0){
						cloudUtils.removeClouds(er.getResult().getTargetId());
					}
				}
			}
		}else{
			flag = 2;
		}
		return flag;
	}


	/**
	 * 添加easyAr
	 * 作者： 张叶
	 * 创建时间：2018年8月23日  上午9:18:23
	 */
	private int insertEasyAr(EasyAr ea, int flag, String imageUrl,
			EasyArResult er) {
		EasyAr easyar = new EasyAr();
		easyar.setTargetId(er.getResult().getTargetId());
		easyar.setImageUrl(imageUrl);
		easyar.setName(ea.getName());
		easyar.setVideoUrl(ea.getVideoUrl());
		easyar.setActive(er.getResult().getActive());
		easyar.setCreateAt(new Date());
		easyar.setAdminId(ea.getAdminId());
		String address = getAddress("38.050802", "114.515151");
		easyar.setAddress(address);
		easyar.setAddressLat("38.050802");
		easyar.setAddressLng("114.515151");
		easyar.setRegionId(3L);
		easyar.setRegionCode("130101");
		easyar.setRegionName("石家庄市");
		easyar.setStatus("on");
		easyar.setType(0);
		Category c = categoryMapper.selectByPrimaryKey(ea.getCategoryId());
		if(c != null && !"".equals(c)){
			easyar.setCategoryId(c.getId());
			easyar.setCategoryCode(c.getCode());
			easyar.setCategoryName(c.getName());	
		}
		this.getRand(easyar);
		if(easyArMapper.insert(easyar) > 0){
			flag = 0;
		}
		return flag;
	}	

	
	/**
	 * 获取经纬度
	 * 作者： 张叶
	 * 创建时间：2018年8月23日  上午9:18:35
	 */
	public String getAddress(String lat, String lng) {
		String region = "";
		try {
			region = AddressUtil.getRegionByLocation(String.format("%s,%s", lng, lat));
		} catch (Exception e) {
			region = "河北省石家庄市";
		}
		return region;
	}
	
	/**
	 * 转Base64
	 * 作者： 张叶
	 * 创建时间：2018年5月15日  下午2:11:11
	 */
	public static String encode(final byte[] bytes) {  
		return new String(Base64.encodeBase64(bytes));  
	}	
	
	
	/**
	 * 删除
	 * 1，删除EasyAR
	 * 2，删除数据库表
	 * 3，删除评论
	 * 4，删除扫描
	 * 5,删除ar_recommend
	 * 作者： 张叶
	 * 创建时间：2018年5月16日  上午10:00:37
	 * @throws Exception 
	 */
	@Transactional
	public int deleted(Long id,Admin admin) throws Exception{
		int flag = 0;
		EasyAr ea = easyArMapper.selectByPrimaryKey(id);
		ea.setOperateAdminId(admin.getId());
		if(ea != null && !"".equals(ea)){
			int type = ea.getType();
			if(type == 0){
				cloudUtils.removeClouds(ea.getTargetId());	
			}
			//删除视频
			ea.setStatus("deleted");
			flag = easyArMapper.updateByPrimaryKey(ea);
			//删除举报评论
			String where = " and easy_ar_id = " + id;
			List<ArComment> list = arCommentMapper.selectByWhere(where);
			if(list.size() > 0){
				String acId = "";
				for(ArComment ac : list){
					acId = acId + ac.getId() + ",";
				}
			}
			//删除评论
			flag += arCommentMapper.deleteByWhere(where);
			
			//删除扫描
			where = " and video_url = '" + ea.getVideoUrl() + "'";
			flag += scanCountMapper.deleteByWhere(where);
			
			//删除扫描
			where = " and easy_ar_id = " + id;
			flag += interviewMapper.deleteByWhere(where);
			
			//删除分享
			where = " and easy_ar_id = " + id;
			flag += infomationMapper.deleteByWhere(where);
			
			//删除举报
			where = " and easy_ar_id = " + id;
			flag += arReportMapper.deleteByWhere(where);
			
			//删除推荐
			where = " and easy_ar_id = " + id;
			flag += arRecommendMapper.deleteByWhere(where);
		}else{
			flag = 1;
		}
		return flag;
	}
	
	
	/**
	 * 修改
	 * 作者： 张叶
	 * 创建时间：2018年5月23日  上午10:11:26
	 */
	public int update(EasyAr ea){
		int flag = 1;
		int type = ea.getType();
		if(type == 0){
			EasyAr old = easyArMapper.selectByPrimaryKey(ea.getId());
			if(old.getName().equals(ea.getName()) && old.getActive().equals(ea.getActive())){
				if(easyArMapper.updateByPrimaryKey(ea) > 0){
					flag = 0;
				}
			}else{
				EasyArResult er = cloudUtils.updateCloud(ea.getTargetId(),ea.getActive().toString(),ea.getName());
				if(er != null && !"".equals(er)){
					String status = er.getStatusCode();
					if(status.equals("0")){//修改成功，改库
						EasyAr easyar = easyArMapper.selectByPrimaryKey(ea.getId());
						easyar.setName(ea.getName());
						easyar.setActive(ea.getActive());
						if(easyArMapper.updateByPrimaryKey(easyar) > 0){
							flag = 0;
						}
					}
				}
			}
		}else{
			if(easyArMapper.updateByPrimaryKey(ea) > 0){
				flag = 0;
			}
		}
		return flag;
	}
	
	
	/**
	 * 添加
	 * 作者： 张叶
	 * 创建时间：2018年7月3日  下午3:57:17
	 */
	public int insert(EasyAr ea){
		if(ea.getStatus() != null && !"".equals(ea.getStatus())){
		}else{
			ea.setStatus("audit");
		}
		this.getRand(ea);
		int i = easyArMapper.insert(ea);
		return i;
	}
	
	
	public int updateRecommend(EasyAr ea){
		int flag = easyArMapper.updateByPrimaryKey(ea);
		return flag;
	}
	
	
	/**
	 * 审核通过
	 * 1，修改status为on
	 * 2，修改active为1
	 * 作者： 张叶
	 * 创建时间：2018年9月6日  下午3:19:33
	 */
	@Transactional
	public int auditEasyAr(Long id,Admin a){
		int flag = 1;
		EasyAr ea = easyArMapper.selectByPrimaryKey(id);
		if(ea != null && !"".equals(ea)){
			ea.setActive(1);
			ea.setStatus("on");
			ea.setAuditAdminId(a.getId());
			ea.setAuditDate(new Date());
			EasyArResult er = cloudUtils.updateCloud(ea.getTargetId(),ea.getActive().toString(),ea.getName());	
			if(er != null && !"".equals(er)){
				String status = er.getStatusCode();
				if(status.equals("0")){//修改成功，改库
					if(easyArMapper.updateByPrimaryKey(ea) > 0){
						flag = 0;
					}
				}
			}
		}
		
		return flag;
	}
	
	@Transactional
	public int auditEasyAr(Long id){
		int flag = 1;
		EasyAr ea = easyArMapper.selectByPrimaryKey(id);
		if(ea != null && !"".equals(ea)){
			ea.setActive(1);
			ea.setStatus("on");
			ea.setAuditDate(new Date());
			if(ea.getType() == 0){
				EasyArResult er = cloudUtils.updateCloud(ea.getTargetId(),ea.getActive().toString(),ea.getName());	
				if(er != null && !"".equals(er)){
					String status = er.getStatusCode();
					if(status.equals("0")){//修改成功，改库
						if(easyArMapper.updateByPrimaryKey(ea) > 0){
							flag = 0;
						}
					}
				}
			}else{
				if(easyArMapper.updateByPrimaryKey(ea) > 0){
					flag = 0;
				}
			}
		}
		
		return flag;
	}
	
	
	/**
	 * 普通视频审核
	 * 作者： 张叶
	 * 创建时间：2018年11月1日  上午10:27:22
	 */
	public int auditEasyArType(Long id,Admin a){
		int flag = 1;
		EasyAr ea = easyArMapper.selectByPrimaryKey(id);
		if(ea != null && !"".equals(ea)){
			ea.setActive(1);
			ea.setStatus("on");
			ea.setAuditAdminId(a.getId());
			ea.setAuditDate(new Date());
			if(easyArMapper.updateByPrimaryKey(ea) > 0){
				flag = 0;
			}
		}
		
		return flag;
	}
	
	
	/**
	 * give  100-200
	 * share 100-200
	 * uploadVideo 50-100
	 * uploadImage  50-100
	 * interviewCount 600-700
	 * 作者： 张叶
	 * 创建时间：2018年11月8日  下午4:55:07
	 */
	private EasyAr getRand(EasyAr ea){
		ea.setGive((int) (Math.random() * (200-100) + 100 + 1));
		ea.setShare((int) (Math.random() * (200-100) + 100 + 1));
		ea.setUploadImage((int) (Math.random() * (100-50) + 50 + 1));
		ea.setUploadVideo((int) (Math.random() * (100-50) + 50 + 1));
		ea.setInterviewCount((int) (Math.random() * (1200-600) + 600 + 1));
//		List<Region> listR = regionMapper.selectByWhere(" and level = 3");
//		Region region = listR.get(this.random(listR.size()));
//		ea.setRegionId(region.getId());
//		ea.setRegionCode(region.getCode());
//		ea.setRegionName(region.getTitle());
		return ea;
	}
	private int random(int max){
		int i = (int) (Math.random() * max);
		return i;
	}
	
	
	
	
	
	
	

	public int updates(EasyAr ea){
		int i = easyArMapper.updateByPrimaryKey(ea);
		return i;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	//旧

	/**
	 * APP搜索，同时添加搜索记录
	 * 作者： 张叶
	 * 创建时间：2018年8月16日  上午8:58:40
	 */
	@Transactional
	public List<Map<String,Object>> selectResult(PageInfo pageInfo, String keywords, String type,Admin admin) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		String where = "";
		String order = " order by create_at desc";
		if(type == null || "".equals(type) || "0".equals(type)){
			where += " (name like '%" + keywords + "%' or info like '%" + keywords + "%')";
			where += " and status = 'on'";
			list = this.selectByPage(pageInfo, where, order);
		}else{
			order = " order by createDate desc";
			where += " username like '%" + keywords + "%'";
			list = adminService.selectByPage(pageInfo,where, order);
			if(list.size() > 0){
				String adminIds = "";
				for(int i = 0 ; i < list.size() ; i ++){
					adminIds = adminIds + list.get(i).get("id") + ",";
				}
				adminIds = adminIds.substring(0,adminIds.length()-1);
				where = " admin_id in (" + adminIds + ")";
			}else{
				where = " 1 = 2";
			}
			order = " order by create_at desc";
			where += " and status = 'on'";
			list = this.selectByPage(pageInfo, where, order);
		}
		SearchLog sl = new SearchLog();
		String wheres = " and name = '" + keywords + "' and type = '" + type + "'";
		if(admin != null){
			sl.setAdminId(admin.getId());
			wheres += " and admin_id = " + admin.getId();
		}
		List<SearchLog> lists = searchLogMapper.selectByWhere(wheres);
		if(lists.size() == 0){
			sl.setName(keywords);
			sl.setCreatedAt(new Date());
			if(type == null || "".equals(type) || "0".equals(type)){
				sl.setType("0");
			}else{
				sl.setType("1");
			}
			searchLogMapper.insert(sl);
		}
		return list;
	}
	
}
